package com.example.daily.ui.navigation

import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.example.daily.ui.auth.LoginScreen
import com.example.daily.ui.auth.RegisterScreen
import com.example.daily.ui.foodrecord.FoodRecordScreen
import com.example.daily.ui.foodrecognition.FoodRecognitionScreen
import com.example.daily.ui.main.MainScreen
import com.example.daily.ui.screen.AiConsultScreen
import java.time.LocalDateTime

@Composable
fun NavGraph(
    navController: NavHostController,
    startDestination: String,
    onAuthSuccess: () -> Unit
) {
    NavHost(
        navController = navController,
        startDestination = startDestination
    ) {
        composable(route = Screen.Login.route) {
            LoginScreen(
                onLoginSuccess = {
                    onAuthSuccess()
                    navController.navigate(Screen.Home.route) {
                        popUpTo(Screen.Login.route) { inclusive = true }
                    }
                },
                onNavigateToRegister = {
                    navController.navigate(Screen.Register.route)
                }
            )
        }

        composable(route = Screen.Register.route) {
            RegisterScreen(
                onRegisterSuccess = {
                    onAuthSuccess()
                    navController.navigate(Screen.Home.route) {
                        popUpTo(Screen.Register.route) { inclusive = true }
                    }
                },
                onNavigateToLogin = {
                    navController.navigate(Screen.Login.route) {
                        popUpTo(Screen.Register.route) { inclusive = true }
                    }
                }
            )
        }

        composable(route = Screen.Home.route) {
            MainScreen()
        }

        composable(route = Screen.FoodRecord.route) {
            FoodRecordScreen(
                navController = navController
            )
        }

        composable(route = Screen.FoodRecognition.route) {
            FoodRecognitionScreen(
                navController = navController,
                onSaveResult = { name, calories ->
                    // 获取FoodRecordViewModel并添加记录
                    navController.previousBackStackEntry
                        ?.savedStateHandle
                        ?.set("food_name", name)
                    navController.previousBackStackEntry
                        ?.savedStateHandle
                        ?.set("food_calories", calories)
                }
            )
        }

        composable(route = Screen.AiConsult.route) {
            AiConsultScreen()
        }
    }
} 